//
//  ClassificationControllerd.m
//  Gift
//
//  Created by dllo on 15/9/10.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ClassificationControllerd.h"
#import "BaseHeader.h"
#import "LDProjectView.h"
#import "LDClassYuanQuan.h"
#import "LDProjectView.h"
#import "LDClassCheckAll.h"
#import "LDSpecialTabView.h"
#import "LDTabHandeView.h"

@interface ClassificationControllerd ()<UITableViewDataSource,UITableViewDelegate,pushClassOne,ButtonCheckPush,imagePusu>

@property (nonatomic, retain)UITableView *tab;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)LDTabHandeView *tabHandeView;
@end

@implementation ClassificationControllerd

// lazy loading
- (NSMutableArray *)array{
    if (!_array) {
        self.array = [[NSMutableArray alloc] init];
    }
    return _array;
}

-(void)dealloc{
    self.tab.delegate = nil;
    self.tab.dataSource = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self.array release];
    [self.tab release];
    [super dealloc];
    
}






//创建tab
- (void)craterTab{
    
    self.tab = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 20)];
    [self.tab registerClass:[LDClassTabCellOne class] forCellReuseIdentifier:@"ClassTabCellOne"];
    
    LDProjectView *project = [[LDProjectView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 120)];
    [self.tab setTableHeaderView:project];
    project.imageDelegate = self;
    project.ButtonCheckdelegate = self;
    self.tab.delegate = self;
    self.tab.dataSource = self;
    self.tab.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tab];

    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"night"];
    if ([passWord isEqualToString:@"yes"]) {
        self.tab.backgroundColor = [UIColor blackColor];
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"lidi" object:nil];
    [self.tab release]; 
    
}

- (void)handleNotification:(NSNotification *)Not{
    NSString *passWord = [Not object];
    if ([passWord isEqualToString:@"yes"]) {
        self.tab.backgroundColor = [UIColor blackColor];

    }if ([passWord isEqualToString:@"no"]) {
        self.tab.backgroundColor = [UIColor whiteColor];

    }
}


#pragma mark - 执行tab代理方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.array.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    self.tabHandeView = [[LDTabHandeView alloc]init];
    self.tabHandeView.backgroundColor = [UIColor colorWithRed:0.981 green:0.910 blue:1.000 alpha:1.000];
    [self.tabHandeView.name setText:[self.array objectAtIndex:section]];
    return self.tabHandeView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 330;
    }else{
        return 185;
    }
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LDClassTabCellOne *cell = [tableView dequeueReusableCellWithIdentifier:@"ClassTabCellOne"];
    cell.Section = indexPath.section;
    cell.delegat = self;
    return cell;
}


- (void)handleData{

    [PFAFNetworking getUrl:@"http://api.liwushuo.com/v2/channel_groups/all" body:nil response:PFJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
            for (NSDictionary *dic in [[resposeObject objectForKey:@"data"] objectForKey:@"channel_groups"])
        {
            NSString *str =[dic objectForKey:@"name"];
            
            [self.array addObject: str];
        }

        [self.tab reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
       
        
    }];
    
}


#pragma mark - 自己写的代理方法
- (void)pushViewCid:(NSNumber *)Cid{
    
    LDClassYuanQuan *vc = [[LDClassYuanQuan alloc]init];
    vc.webInt = Cid;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

- (void)hadleButtonCheck{
    
    LDClassCheckAll *checkVC = [[LDClassCheckAll alloc]init];
    [self.navigationController pushViewController:checkVC animated:YES];
   
    [checkVC release];
}

- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
    if (self.array.count == 0) {
        [self.tab reloadData];
    }

}

- (void)handleImagePushNum:(NSNumber *)number{
    
    LDSpecialTabView *special = [[LDSpecialTabView alloc]init];
    special.number = number;
    [self.navigationController pushViewController:special animated:YES];
    
    [special release];
}






- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"分类";
    self.navigationController.title = @"分类";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.], NSForegroundColorAttributeName:[UIColor colorWithRed:1.000 green:1.00 blue:1.000 alpha:1.000]}];
    [self craterTab];
    [self handleData];
    
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
